package pos.Configuration;

import java.util.HashMap;
import javax.vecmath.Vector3d;
import pos.ControlCenter;
import pos.ICommand;
import pos.IModeController;
import pos.MessageBox.MessageBox;
import pos.Profile;
import pos.utils.IContainer;

/**
 *
 * @author plus
 */
public class ConfigController implements IModeController{

    private HashMap<String, ICommand> CommandsPool = new HashMap<String, ICommand>();
    private static ConfigSurrendings Surrendings;
    private static ConfigMenu Menu;
    Profile CurrentProfile;
    
    public ConfigController() {
        CurrentProfile=new Profile();
        if (Surrendings==null || Menu==null)
            initObjects();
        else
            Menu.setMyController(this);
        BlendCommands();
    }

    public Object doEvent(String EventName, Object Param) {
        ICommand InvokedCommand=CommandsPool.get(EventName);
        if (InvokedCommand!=null)
            return InvokedCommand.execute(Param);
        else{
            System.err.println("Event:"+EventName+" NOT found!");
            return null;
        }
    }

    public void setMode() {
        Menu.fixCurrentDockItem();
        
        ControlCenter.setView(
                new Vector3d(0,0,0), new Vector3d(0,0,0));
        ControlCenter.eventBehavior.setTarget(Menu, this);
        
        ControlCenter.objRoot.addChild(Surrendings);
        ControlCenter.dashBoardRoot.addChild(Menu);
        ControlCenter.preFrameInvoker.addLisener(Menu);
    }
    
    
    public void disposeMode() {
        ControlCenter.preFrameInvoker.removeLisener(Menu);
        Surrendings.detach();
        //Menu.detach();
        ControlCenter.dashBoardRoot.removeAllChildren();
    }

    private void initObjects(){
        Surrendings=new ConfigSurrendings();
        Menu=new ConfigMenu(this);
    }
    /**
     * 将事件名与Command对象绑定
     */
    private void BlendCommands() {
        CommandsPool.put("KeyEvent", new CommandKeyEvent((IContainer)Menu));
        CommandsPool.put("Save", new CommandSave(CurrentProfile));
        CommandsPool.put("CancelChange", new CommandCancel());
        CommandsPool.put("CancelMsg", new pos.CommandMsgbox(
                new MessageBox(null,null,"取消更改","您确定丢弃对设置的改动吗？",
                new String[]{"是的，我不想改","不,再考虑一下"},
                new String[]{"CancelChange","Cancel"})));
        CommandsPool.put("ProfileSet", new CommandProfileSet(CurrentProfile));
    }

}
